﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
///DBAccessTran 的摘要说明
/// </summary>
public class DBAccessTran : IDisposable
{
    public static readonly string _connectionString = DBAccess._connectionString;
    private SqlConnection _Conn;
    private SqlTransaction _Tran;

    public DBAccessTran()
    {
        _Conn = new SqlConnection(_connectionString);
        _Conn.Open();
        _Tran = _Conn.BeginTransaction();
    }
    public void Dispose()
    {
        _Conn.Close();
        _Conn.Dispose();
    }
    public void Commit()
    {
        _Tran.Commit();
    }
    public void Rollback()
    {
        _Tran.Rollback();
    }
    // ExecuteNonQuery
    public int ExecuteNonQuery(string commandText)
    {
        DBAccess.CheckCommandText(commandText);
        return SqlHelper.ExecuteNonQuery(_Tran, CommandType.Text, commandText); //3
    }
    public int ExecuteNonQuery(string commandText, params SqlParameter[] cmdParameters)
    {
        DBAccess.CheckCommandText(commandText);
        return SqlHelper.ExecuteNonQuery(_Tran, CommandType.Text, commandText, cmdParameters); //8
    }
    public int ExecuteNonQueryProc(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteNonQuery(_Tran, procName, cmdParameters); //4
    }
    public int ExecuteNonQueryProcOut(string procName, params SqlParameter[] cmdParameters)
    {
        return SqlHelper.ExecuteNonQuery(_Tran, CommandType.StoredProcedure, procName, cmdParameters); //8
    }
    public DataSet ExecuteDataset(string commandText)
    {
        DBAccess.CheckCommandText(commandText);
        // return SqlHelper.ExecuteDataTable(_Conn, CommandType.Text, commandText, null);
        return SqlHelper.ExecuteDataset(_Tran, CommandType.Text, commandText);

    }
    public object ExecuteScalar(string commandText, params SqlParameter[] cmdParameters)
    {
        DBAccess.CheckCommandText(commandText);
        return SqlHelper.ExecuteScalar(_Tran, CommandType.Text, commandText, cmdParameters);
    }
    public DataSet ExecuteDataset(string commandText, params SqlParameter[] cmdParameters)
    {
        DBAccess.CheckCommandText(commandText);
        return SqlHelper.ExecuteDataset(_Tran, CommandType.Text, commandText, cmdParameters);
    }
}
